<?=view('header.html')?>

<body class="layui-view-bodys">
    <div class="layui-form-iframe">
        <div class="layui-row">
            <div class="layui-card-body">
                <div class="form-box">
                    <blockquote class="site-text layui-elem-quote">
                        您正在编辑/设置 <span class="layui-badge layui-bg-blue">[<?=$role['name']?>]</span> 角色的权限
                    </blockquote>

                    <form class="layui-form mokui-form-md3" method="post" id="myform">
                        
                        <?php if($rule):?>
                        <?php foreach($rule as $top):?>
                        <table class="layui-table pid_<?=$top['id']?>" lay-size="sm">
                            <thead>
                                <tr>
                                    <th colspan="2"><?=$top['name']?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if(isset($top['child']) && $top['child']):?>
                                <?php foreach($top['child'] as $child):?>
                                <tr>
                                    <td><input type="checkbox" value="<?=$child['id']?>" lay-filter="checkall" data-id="<?=$child['id']?>" class="pid_<?=$child['id']?> checkall" name="rule[]" lay-skin="primary" title="<?=$child['name']?>(全选)" <?=in_array($child['uri'], $auth) ? 'checked' : ''?>> &nbsp;&nbsp;</td>
                                    <td>
                                        <?php if(isset($child['child']) && $child['child']):?>
                                        <?php foreach($child['child'] as $son):?>
                                        <input type="checkbox" class="pids_<?=$child['id']?> checkone" lay-filter="checkone" value="<?=$son['id']?>" data-pid="<?=$son['pid']?>" name="data[]" lay-skin="primary" title="<?=$son['name']?>" <?=(in_array($son['uri'], $auth) || $son['skip']==1) ? 'checked' : ''?>>
                                        <?php endforeach;?>
                                        <?php endif;?>
                                    </td>
                                </tr>
                                <?php endforeach;?>
                                <?php endif;?>
                            </tbody>
                        </table>
                        <hr class="layui-bg-green">
                        <?php endforeach;?>
                        <?php endif;?>

                        <div class="layui-form-item">
                            <label class="layui-form-label"></label>
                            <div class="layui-input-block">
                                <button class="layui-btn layui-btn-blue" type="button" lay-submit="" lay-filter="submitSave">提交保存</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <?=view('footer.html')?>

    <script type="text/javascript">

        layui.use(['layer', 'jquery', 'form'], function () {
            var layer = layui.layer,
                $ = layui.jquery,
                form = layui.form;
            

            form.on('checkbox(checkall)', function (data) {
                var pid = $(data.elem).attr('data-id');
                var ischecked = $(this).prop("checked");
                if (ischecked == false) {
                    $(".pids_" + pid).prop('checked', false);
                } else if (ischecked == true) {
                    $(".pids_" + pid).prop('checked', true);
                }
                form.render('checkbox');
            });

            form.on('checkbox(checkone)', function (data) {
                var that = this;
                var pid = $(data.elem).attr('data-pid');
                var ischecked = $(that).prop("checked");
                var count = 0;
                $(".pids_" + pid).each(function (i) {
                    if ($(this).prop('checked') == true) {
                        count++;
                    }
                });
                if (count == 0) {
                    $(".pid_" + pid).prop('checked', false);
                } else {
                    $(".pid_" + pid).prop('checked', true);
                }
                form.render('checkbox');
            });

            //提交保存
            form.on('submit(submitSave)', function (data) {
                var layindex = layer.load(2, { time: 8000 });
                var url = window.location.href;
                $.post(url, data.field, function (res) {
                    layer.close(layindex);
                    if (res.code == 0) {

                        // var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        // parent.location.href = adminpath + 'admin/role/index';
                        // parent.location.reload();
                        // parent.layer.close(index);
                        layer.msg(res.msg, { icon: 1, time: 1500 }, function () {
                            window.location.reload();
                            // location.href = adminpath + 'admin/combo/index';
                        });

                    } else {
                        layer.msg(res.msg, { icon: 5, time: 1500 });
                    }
                }, 'json');
            });

        });

    </script>

</body>

</html>
